<?php

namespace App\Notifications\OrderNotifications;

use App\Common\Constant\Params;
use App\Models\Master;
use App\Models\Merchant;
use App\Models\Order;
use App\Models\OrderRefund;
use App\Notifications\Channels\SmsChannel;
use App\Notifications\OrderNotification;
use App\Services\IMasterService;
use Illuminate\Bus\Queueable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class OrderArbitrateNotification extends Notification implements OrderNotification
{
    use Queueable;

    private $refund;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(OrderRefund $refund)
    {
        $this->refund = $refund;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database',SmsChannel::class];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray(Model $notifiable)
    {
        if ($notifiable instanceof Master){
            return $this->messageToMaster($notifiable);
        }
    }

    public function messageToMaster(Master $master)
    {
        if ($this->refund->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_DISAGREE){//不同意退款
            $message = <<<EOT
%s师傅，您好！您服务的订单%s，经过客服调查核实，仲裁结果不支持退款。
EOT;

        }else if ($this->refund->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_AGREE_FULL){//全额退款
            $message = <<<EOT
%s师傅，您好！您服务的订单%s，经过客服调查核实，仲裁结果支持退款(全部退款)。
EOT;
        }else if ($this->refund->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_AGREE_PART){//部分退款
            $message = <<<EOT
%s师傅，您好！您服务的订单%s，经过客服调查核实，仲裁结果支持退款(部分退款)。
EOT;
        }
        $message = sprintf($message,
            $master->real_name,
            $this->refund->order->order_number);
        return [
            'title'=>'双方纠纷仲裁结果提醒',
            'order_id'=>$this->refund->order->id,
            'message'=>$message
        ];
    }

    /**
     * 发送短信
     * @param Master $master
     * @return array($phoneNum,$message)
     */
    public function toSms(Model $notifiable)
    {
        if ($notifiable instanceof Merchant){
            return $this->smsToMerchant($notifiable);
        }
    }

    public function smsToMerchant(Merchant $merchant)
    {
        if ($this->refund->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_DISAGREE){//不同意退款
            $message = <<<EOT
【%s】您的仲裁申请订单%s，经过客服核实调查，仲裁不同意退款，仲裁详情可在退款列表查看。如有疑问请致电0779-3931900
EOT;
        }else if ($this->refund->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_AGREE_FULL){//全额退款
            $message = <<<EOT
【%s】您的仲裁申请订单%s，客服仲裁同意退款(全部退款)，仲裁详情可在退款列表查看。如有疑问请致电0779-3931900
EOT;
        }else if ($this->refund->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_AGREE_PART){//部分退款
            $message = <<<EOT
【%s】您的仲裁申请订单%s，客服仲裁同意退款(部分退款)，仲裁详情可在退款列表查看。如有疑问请致电0779-3931900
EOT;
        }
        $message = sprintf($message,
            config('app.name'),
            $this->refund->order->order_number);
        return [$merchant->me_phone,$message];
    }
}
